package com.cogent.business.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.cogent.business.action.AbstractAction;
import com.cogent.business.domain.WebUserDomain;
import com.cogent.business.helper.BussinessDealHelper;
import com.cogent.business.helper.UserDomainHelper;
import com.cogent.common.date.JodaDateUtil;
import com.cogent.common.util.Globals;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dao.domain.Pagination;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.type.QueryType;

public class WebAction extends AbstractAction {
	
	public String index() {
		
		return "index";
	}
	
	public String partyviewlist() {
		HashMap formMap = getFormMapWithUser();
		String sql = "select id, title from web_newscontent where menutype=10 and status=0 order by sequence desc";
		List menuList = BussinessQueryHelper.query("web_newscontent", sql, bussinessFacade);
		String id =  MapUtil.getStringValue(formMap, "id");
		HashMap datamap = new HashMap();
		if(StringUtil.isEmpty(id)) {
			if(menuList.size() > 0) {
				HashMap curMap = (HashMap)menuList.get(0);
				id = MapUtil.getStringValue(curMap, "id");				
			}
		}
		if(!StringUtil.isEmpty(id)) {
			sql = "select * from web_newscontent where id='" + id + "'";
			datamap = BussinessQueryHelper.queryOneRow("web_newscontent", sql, bussinessFacade);
		}
		setResponseMsg("menulist", menuList);
		setResponseMsg("datamap", datamap);
		setResponseMsg("formmap", formMap);
		
		return "partyviewlist";
	}
	
	public String viewlist2() {
		HashMap formMap = getFormMapWithUser();
		String sql = "select id, title from web_newscontent where menutype=10 and status=0 order by sequence desc";
		List menuList = BussinessQueryHelper.query("web_newscontent", sql, bussinessFacade);
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("menutype", "20"));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("web_newscontent", queryList, " order by sequence desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);		
		setResponseMsg("menulist", menuList);
		setResponseMsg("formmap", formMap);
		
		return "viewlist2";
	}
	
	public String affairviewlist() {
		HashMap formMap = getFormMapWithUser();
		String menutype = MapUtil.getStringValue(formMap, "menutype", "11");
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("menutype", menutype));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("web_newscontent", queryList, " order by sequence desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "affairviewlist";
	}
	
	public String partylist() {
		HashMap formMap = getFormMapWithUser();
		String menutype = MapUtil.getStringValue(formMap, "menutype", "1");
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("menutype", menutype));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("web_newscontent", queryList, " order by sequence desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "partylist";
	}
	
	public String servicelist() {
		HashMap formMap = getFormMapWithUser();
		String menutype = MapUtil.getStringValue(formMap, "menutype", "4");
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("menutype", menutype));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("web_newscontent", queryList, " order by sequence desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "servicelist";
	}
	
	public String arealist() {
		HashMap formMap = getFormMapWithUser();		
		String stype = MapUtil.getStringValue(formMap, "stype", "1");
		String tableName = "web_areaplan";
		String orderby = " order by joindate desc";
		if(stype.equals("2")) {
			tableName = "web_areanewscontent";
			orderby = " order by sequence desc";
		}
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain(tableName, "status", "0"));
		if(stype.equals("2"))
			queryList.add(new QueryParamDomain(tableName, "menutype", MapUtil.getStringValue(formMap, "menutype", "1")));
		Pagination pagination = bussinessFacade.queryPaged(tableName, queryList, orderby, MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "arealist";
	}
	
	public String memberlist() {
		HashMap formMap = getFormMapWithUser();		
		String stype = MapUtil.getStringValue(formMap, "stype", "1");
		String tableName = "web_partyplan";
		String orderby = " order by joindate desc";
		List queryList = new ArrayList();
		if(stype.equals("2")) {
			tableName = "web_partynewscontent";
			orderby = " order by sequence desc";
		} else if(stype.equals("3")) {
			tableName = "web_newscontent";
			orderby = " order by sequence desc";
			queryList.add(new QueryParamDomain("menutype", "19"));
		}
		queryList.add(new QueryParamDomain(tableName, "status", "0"));
		Pagination pagination = bussinessFacade.queryPaged(tableName, queryList, orderby, MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "memberlist";
	}
	
	public String memberview() {
		HashMap formMap = getFormMapWithUser();
		String stype = MapUtil.getStringValue(formMap, "stype", "1");
		String tableName = "web_areanewscontent";
		if(MapUtil.isEmpty(formMap, "menutype"))
			tableName = "web_partynewscontent";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID(tableName, queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);
		
		return "memberview";
	}
	
	public String studylist() {
		HashMap formMap = getFormMapWithUser();
		String menutype = MapUtil.getStringValue(formMap, "menutype", "1");
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("menutype", menutype));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("web_studycontent", queryList, " order by sequence desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "studylist";
	}
	
	public String notelist() {
		HashMap formMap = getFormMapWithUser();
		String menutype = MapUtil.getStringValue(formMap, "menutype", "9");
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("menutype", menutype));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("web_newscontent", queryList, " order by sequence desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "notelist";
	}
	
	public String view() {
		HashMap formMap = getFormMapWithUser();
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID("web_newscontent", queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);
		
		return "view";
	}
	
	public String studyview() {
		HashMap formMap = getFormMapWithUser();
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID("web_studycontent", queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);
		
		return "studyview";
	}
	
	public String listmsg() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		HashMap formMap = getFormMapWithUser();
		String menutype = MapUtil.getStringValue(formMap, "type", "1");
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("menutype", menutype));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("party_formmsg", queryList, " order by credate desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "listmsg";
	}
	
	public String listmymsg() {
		HashMap formMap = getFormMapWithUser();
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		int type = MapUtil.getIntValue(formMap, "type", 1);
		List queryList = new ArrayList();
		if(type == 1)
			queryList.add(new QueryParamDomain("menutype", "3", QueryType.LESS));	
		else
			queryList.add(new QueryParamDomain("menutype", "2", QueryType.GREATER));
		queryList.add(new QueryParamDomain("memid", userDomain.getId()));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("party_formmsg", queryList, " order by credate desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "listmymsg";
	}
	
	public String mydesk() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("organid", userDomain.getOrganid()));
		queryList.add(new QueryParamDomain("status", "0"));
		List pageList = bussinessFacade.queryWithPostfix("party_member", queryList, " order by sequence desc");
		setResponseMsg("pagelist", pageList);
		return "mydesk";		
	}
	
	public String mystudy() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		HashMap formMap = getFormMapWithUser();
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("status", "0"));
		queryList.add(new QueryParamDomain("isneed", "0"));
		Pagination pagination = bussinessFacade.queryPaged("web_studycontent", queryList, " order by sequence desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "mystudy";
	}
	
	public String savemsg() {
		HashMap formMap = getFormMapWithUser();
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		formMap.put("organid", userDomain.getOrganid());
		formMap.put("creator", userDomain.getRealname());
		formMap.put("modifier", userDomain.getRealname());		
		formMap.put("organcode", userDomain.getOrgancode());
		formMap.put("status", "0");
		formMap.put("memid", userDomain.getId());
		bussinessFacade.save("party_formmsg", formMap);
		setResponseUrl(formMap, "Web_listmsg.action?type=" + MapUtil.getStringValue(formMap, "menutype"));
		
		return null;
	}
	
	public String savemymsg() {
		HashMap formMap = getFormMapWithUser();
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		formMap.put("organid", userDomain.getOrganid());
		formMap.put("creator", userDomain.getRealname());
		formMap.put("modifier", userDomain.getRealname());		
		formMap.put("organcode", userDomain.getOrgancode());
		formMap.put("status", "0");
		formMap.put("memid", userDomain.getId());
		int menutype = MapUtil.getIntValue(formMap, "menutype");
		if(menutype <3)
			menutype = 1;
		else 
			menutype = 2;
		bussinessFacade.save("party_formmsg", formMap);
		setResponseUrl(formMap, "Web_listmymsg.action?type=" + menutype);
		
		return null;
	}
	
	public String weblink() {
		String sql = "select * from web_contentlinkmenu where status=0 order by sequence asc";
		List menuList = BussinessQueryHelper.query("web_contentlinkmenu", sql, bussinessFacade);
		HashMap menudatamap = new HashMap();
		Iterator it = menuList.iterator();
		while(it.hasNext()) {
			HashMap datamap = (HashMap)it.next();
			String menuid = MapUtil.getStringValue(datamap, "id");
			sql = "select * from web_contentlink where status=0 and menuid='" + menuid + "' order by sequence asc";
			List linkList = BussinessQueryHelper.query("web_contentlink", sql, bussinessFacade);
			menudatamap.put(menuid, linkList);
		}
		setResponseMsg("menulist", menuList);
		setResponseMsg("menudatamap", menudatamap);
		
		return "weblink";
	}
	
	public String partyindex() {
	  WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		setResponseMsg("logintime",""+ (JodaDateUtil.getCurSystemDate().getTime()-userDomain.getLogintime())/1000);
		setResponseMsg("alllogintime",""+ (60*60*2+(JodaDateUtil.getCurSystemDate().getTime()-userDomain.getLogintime())/1000));
		
		return "partyindex";		
	}
	
	public String partylife() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		
		return "partylife";
	}
	
	public String partystudylist() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		HashMap formMap = getFormMapWithUser();
		String menutype = MapUtil.getStringValue(formMap, "menutype", "1");
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("menutype", menutype));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("web_studycontent", queryList, " order by sequence desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "partystudylist";
	}
	
	public String netviewlist() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		HashMap formMap = getFormMapWithUser();
		String sql = "select id, title from web_newscontent where menutype=12 and status=0 order by sequence desc";
		List menuList = BussinessQueryHelper.query("web_newscontent", sql, bussinessFacade);
		String id =  MapUtil.getStringValue(formMap, "id");
		HashMap datamap = new HashMap();
		if(StringUtil.isEmpty(id)) {
			if(menuList.size() > 0) {
				HashMap curMap = (HashMap)menuList.get(0);
				id = MapUtil.getStringValue(curMap, "id");				
			}
		}
		if(!StringUtil.isEmpty(id)) {
			sql = "select * from web_newscontent where id='" + id + "'";
			datamap = BussinessQueryHelper.queryOneRow("web_newscontent", sql, bussinessFacade);
		}
		setResponseMsg("menulist", menuList);
		setResponseMsg("datamap", datamap);
		setResponseMsg("formmap", formMap);
		
		return "netviewlist";
	}
	
	public String netserviceviewlist() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		HashMap formMap = getFormMapWithUser();
		String sql = "select id, title from web_newscontent where menutype=14 and status=0 order by sequence desc";
		List menuList = BussinessQueryHelper.query("web_newscontent", sql, bussinessFacade);
		String id =  MapUtil.getStringValue(formMap, "id");
		HashMap datamap = new HashMap();
		if(StringUtil.isEmpty(id)) {
			if(menuList.size() > 0) {
				HashMap curMap = (HashMap)menuList.get(0);
				id = MapUtil.getStringValue(curMap, "id");				
			}
		}
		if(!StringUtil.isEmpty(id)) {
			sql = "select * from web_newscontent where id='" + id + "'";
			datamap = BussinessQueryHelper.queryOneRow("web_newscontent", sql, bussinessFacade);
		}
		setResponseMsg("menulist", menuList);
		setResponseMsg("datamap", datamap);
		setResponseMsg("formmap", formMap);
		
		return "netserviceviewlist";
	}
	
	public String savejoin() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		HashMap formMap = getFormMapWithUser();
		HashMap retMap = new HashMap();
		if(userDomain == null)
			retMap.put("errmsg", "您没有登录，请登录后操作!");
		else {
			List queryList = new ArrayList();
			queryList.add(new QueryParamDomain("planid", MapUtil.getStringValue(formMap, "id")));
			queryList.add(new QueryParamDomain("joinid", userDomain.getId()));
			List joinList = bussinessFacade.query("web_networkplanperson", queryList);
			if(joinList.size() > 0)
				retMap.put("errmsg", "您已经报名参加该活动，不能重复参加!");
			else {
				HashMap saveMap = new HashMap();
				saveMap.put("planid", MapUtil.getStringValue(formMap, "id"));
				saveMap.put("joinid", userDomain.getId());
				saveMap.put("joinname", userDomain.getRealname());
				BussinessDealHelper.copyUserInfo(saveMap, formMap);
				bussinessFacade.save("web_networkplanperson", saveMap);
				retMap.put("errmsg", "您已成功报名参加该活动，请准时参加!");
			}
		}
		String message = MapUtil.mapToJsonStr(retMap);
		responseMsg(message);
		
		return null;
	}
	
	public String netview() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		HashMap formMap = getFormMapWithUser();
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID("web_newscontent", queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);
		
		return "netview";
	}
	
	public String partynetlist() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		HashMap formMap = getFormMapWithUser();
		String menutype = MapUtil.getStringValue(formMap, "menutype", "12");
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("menutype", menutype));		
		queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged("web_newscontent", queryList, " order by sequence desc", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "partynetlist";
	}
	
	public String volunteerlist() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		
		return "volunteerlist";
	}
	
	public String democracylist() {
		WebUserDomain userDomain = UserDomainHelper.getWebUser(ServletActionContext.getRequest());
		if(userDomain == null) {
			setResponseUrl("Web_index.action");
			return null;
		}
		
		return "democracylist";
	}
}